<template>
  <div class="page-mobile_study-level">
    <div :class="['header-top', scroll > 0 ? 'active' : '']">
      <div class="header_left" @click="$router.back()">
        <van-icon name="arrow-left" style="margin-left:.2rem;" />
      </div>
      <div class="center_title">学习等级</div>
      <div class="header_right"></div>
    </div>
    <div class="fixed-box"></div>
    <div class="user-level">
      <div class="avatar">
        <img v-if="user_info.avatar" v-lazy="user_info.avatar" :key="user_info.avatar" />
        <img v-else src="/img/mobile/default-avatar.png" alt="" />
      </div>
      <div class="u-title">您当前的等级是</div>
      <div class="u-level">
        {{ user_info.studyStarNum | formatStudyStar(that) }}
      </div>
      <div class="u-xin"><van-rate v-model="starValue" readonly /></div>
      <div class="u-tips" v-if="user_info.studyStarNum >= 25">
        <div>您已达到最高等级！</div>
      </div>
      <div class="u-tips" v-else>
        <div>
          您已学习 <span>{{ oldStudyTime }}</span> 个小时,
        </div>
        <div>
          还需学习 <span>{{ newStudyTime }}</span> 个小时或完成
          <span>5</span> 场考试即可升级为{{
            (user_info.studyStarNum + 5) | formatStudyStar(that)
          }}等级哦
        </div>
      </div>
      <div class="inline"></div>
      <div class="explain">
        <span>{{ user_info.studyStarNum | formatStudyStar(that) }}：</span
        >{{ user_info.studyStarNum | formatStudyLevelExplain(that) }}
      </div>
      <div class="rule-explain">
        <div class="title">规则说明</div>
        <div class="content">
          {{ studyLevelConfig.studyLevelExplain }}
        </div>
      </div>
    </div>
    <div class="star-log">
      <div class="log-item" v-for="(item,index) in studyLevelRecord" :key="index">
        <div class="log-left">{{item.title}}</div>
        <div class="log-right">+1</div>
      </div>
      <van-empty description="暂无学习记录" v-show="studyLevelRecord.length == 0" />
      <div class="footer" v-show="studyLevelRecord.length != 0">
        <span class="tips">我是有底线的</span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import WhiteHeader from "~/components/mobile/WhiteHeader.vue";
import {
  getStudyLevel,
  getStudyLevelConfig,
  getStudyLevelRecord
} from "~/api/study-level";
export default {
  components: {
    WhiteHeader
  },
  scrollToTop: true,
  data() {
    return {
      that: this,
      scroll: 0,
      studyLevelList: [], //学习等级列表
      studyLevelConfig: {}, //学习等级配置
      studyLevelRecord: [], //学习记录
      pageSize: 10,
      page: 1,
      total: 0
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      is_loading(state) {
        return state.is_loading;
      }
    }),
    starValue() {
      if (this.user_info.studyStarNum >= 25) {
        return 5;
      } else {
        return this.user_info.studyStarNum % 5;
      }
    },
    oldStudyTime() {
      return this.starValue * this.studyLevelConfig.studyTime;
    },
    newStudyTime() {
      return (5 - this.starValue) * this.studyLevelConfig.studyTime;
    }
  },
  watch: {},
  filters: {
    formatStudyStar(val, that) {
      if (that.studyLevelList.length > 0) {
        if (val >= 0 && val < 5) {
          return that.studyLevelList[0]["designation"];
        } else if (val >= 5 && val < 10) {
          return that.studyLevelList[1]["designation"];
        } else if (val >= 10 && val < 15) {
          return that.studyLevelList[2]["designation"];
        } else if (val >= 15 && val < 20) {
          return that.studyLevelList[3]["designation"];
        } else if (val >= 20) {
          return that.studyLevelList[4]["designation"];
        } else {
          return "未知";
        }
      } else {
        return "未知";
      }
    },
    formatStudyLevelExplain(val, that) {
      if (that.studyLevelList.length > 0) {
        if (val >= 0 && val < 5) {
          return that.studyLevelList[0]["introduce"];
        } else if (val >= 5 && val < 10) {
          return that.studyLevelList[1]["introduce"];
        } else if (val >= 10 && val < 15) {
          return that.studyLevelList[2]["introduce"];
        } else if (val >= 15 && val < 20) {
          return that.studyLevelList[3]["introduce"];
        } else if (val >= 20) {
          return that.studyLevelList[4]["introduce"];
        } else {
          return "未知";
        }
      } else {
        return "未知";
      }
    }
  },
  activated() {
    if (!this.token && Object.keys(this.user_info).length == 0) {
      this.$router.replace("/m/login");
    }
    if (process.client) {
      window.addEventListener("scroll", this.eventListener);
    }
    this.getData();
    this.getStudyLevelList();
    this.getStudyLevelConfig();
  },
  deactivated() {
    if (process.client) {
      window.removeEventListener("scroll", this.eventListener);
    }
  },
  destroyed() {
    if (process.client) {
      window.removeEventListener("scroll", this.eventListener);
    }
  },
  created() {
    if (!this.token && Object.keys(this.user_info).length == 0) {
      return this.$router.replace("/m/login");
    }
    this.getData();
    this.getStudyLevelList();
    this.getStudyLevelConfig();
  },
  mounted() {
    if (process.client) {
      window.addEventListener("scroll", this.eventListener);
    }
  },
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    async getStudyLevelList() {
      this.SET_IS_LOADING(true);
      let result = await getStudyLevel();
      if (result && result.data && result.data.code === 200) {
        this.studyLevelList = result.data.data.rows;
      } else {
        this.$toast("获取失败！");
      }
      this.SET_IS_LOADING(false);
    },
    async getStudyLevelConfig() {
      this.SET_IS_LOADING(true);
      let result = await getStudyLevelConfig(1);
      if (result && result.data && result.data.code === 200) {
        this.studyLevelConfig = result.data.data;
      } else {
        this.$toast("获取失败！");
      }
      this.SET_IS_LOADING(false);
    },
    toPath(path) {
      this.$router.push(path);
    },

    throttle(fn, delay) {
      let lastTime = 0;
      return !(function() {
        let nowTime = Date.now();
        if (nowTime - lastTime > delay) {
          fn.call(this);
          lastTime = nowTime;
        }
      })();
    },
    pullUp() {
      // 滚动条离顶部的距离
      let scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      // 窗口可视区域高度
      let clientHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      // 滚动区域高度
      let scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      // 滚动条 + 可视区域高度 >= 滚动条高度 ==>  滚动条触底
      if (scrollTop + clientHeight >= scrollHeight) {
        console.log("上拉加载");
        if (this.total <= this.studyLevelRecord.length) {
          return;
        }
        if (this.is_loading) {
          return;
        }
        this.page++;
        this.getData("pullup");
      }
    },
    eventListener() {
      this.throttle(this.pullUp, 300);
    },
    async getData(fetch_type) {
      this.SET_IS_LOADING(true);
      let result = await getStudyLevelRecord({
        sort: "id desc",
        offset: this.pageSize * (this.page - 1),
        limit: this.pageSize,
        uid: this.user_info.id
      });
      if (result && result.data.code == 200) {
        if (fetch_type == "pullup") {
          this.studyLevelRecord = [
            ...this.studyLevelRecord,
            ...result.data.data.rows
          ];
        } else {
          this.studyLevelRecord = result.data.data.rows;
        }
        this.total = result.data.data.total;
        this.SET_IS_LOADING(false);
      } else {
        this.SET_IS_LOADING(false);
        this.$toast("获取失败！");
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.page-mobile_study-level {
  //background-color: #f7f7fa;
  background: var(--theme-color);
  width: 7.2rem;
  min-height: 100vh;
  //padding-top: 2rem;
  .header-top {
    width: 7.2rem;
    height: 0.9rem;
    z-index: 999999;
    position: fixed;
    top: 0;
    left: 0;
    line-height: 0.9rem;
    display: flex;
    align-items: center;
    box-shadow: none;
    background: var(--theme-color);
    .header_left {
      width: 15%;
      color: #ffffff;
      font-size: 0.35rem;
      display: flex;
      align-items: center;
    }
    .center_title {
      width: 70%;
      text-align: center;
      font-size: 0.3456rem;
      color: #ffffff;
      font-weight: bold;
    }
    .header_right {
      width: 15%;
      img {
        margin: 0 auto;
        display: block;
        width: 0.6111rem;
        height: 0.6111rem;
      }
    }
  }
  .header-top.active {
    background: var(--theme-color);
  }
  .fixed-box {
    width: 7.2rem;
    height: 0.9rem;
  }
  .user-level {
    width: 6.624rem;
    height: 9.008rem;
    background-color: #ffffff;
    border-radius: 0.192rem;
    margin: 1.5rem auto 0;
    position: relative;
    text-align: center;
    .avatar {
      width: 1.536rem;
      height: 1.536rem;
      overflow: hidden;
      border-radius: 50%;
      border: 1px solid #ffffff;
      position: absolute;
      left: 50%;
      top: 0;
      transform: translate(-50%, -50%);
      background-color: #ffffff;
      img {
        width: 100%;
        height: 100%;
        display: block;
      }
    }
    .u-title {
      font-size: 0.288rem;
      font-weight: bold;
      padding-top: 1.1rem;
      padding-bottom: 0.1rem;
      color: #4a4b4d;
    }
    .u-level {
      font-size: 0.4608rem;
      font-weight: bold;
      color: var(--theme-color);
      line-height: 0.8rem;
      position: relative;
      &::after {
        content: "";
        display: block;
        width: 0.3rem;
        height: 0.05rem;
        position: absolute;
        background: var(--theme-color);
        left: 50%;
        bottom: 0;
        transform: translateX(-50%);
      }
    }
    .u-xin {
      margin: 0.3rem 0;
      color: var(--theme-color);
    }
    .u-tips {
      font-size: 0.288rem;
      font-weight: bold;
      color: #4a4b4c;
      padding: 0 1rem;
      height: 1.056rem;
      span {
        color: var(--theme-color);
      }
    }
    .inline {
      margin-top: 0.5rem;
      width: 100%;
      height: 1px;
      background-color: #e3e4e6;
    }
    .explain {
      padding: 0.3rem 0.5rem 0 0.5rem;
      line-height: 0.4rem;
      height: 1.5rem;
      font-size: 0.288rem;
      text-align: left;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-box-orient: vertical;
      span {
        color: var(--theme-color);
      }
    }
    .rule-explain {
      padding: 0.3rem 0.5rem 0 0.5rem;
      .title {
        font-size: 0.288rem;
        color: var(--theme-color);
        margin-bottom: 0.2rem;
      }
      .content {
        text-align: left;
        font-size: 0.288rem;
        line-height: 0.4rem;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-box-orient: vertical;
      }
    }
  }
  .star-log {
    width: 6.624rem;
    min-height: 4rem;
    background-color: #ffffff;
    border-top-left-radius: 0.192rem;
    border-top-right-radius: 0.192rem;
    margin: 0.2rem auto 0;
    .log-item {
      width: 100%;
      display: flex;
      justify-content: space-between;
      height: 1.44rem;
      line-height: 1.44rem;
      box-sizing: border-box;
      padding: 0 0.4rem;
      font-size: 0.288rem;
      font-weight: bold;
      border-bottom: 1px solid #e3e4e6;
      .log-left {
        color: #4a4b4c;
      }
      .log-right {
        color: #f39090;
      }
    }
    .log-item:last-child {
      border: none;
    }
    .footer {
      padding: 0.3rem;
      font-size: 0.26rem;
      color: #babec2;
      text-align: center;
      .tips {
        position: relative;
      }
      .tips::before {
        content: "";
        display: block;
        position: absolute;
        left: -1.7rem;
        top: 50%;
        transform: translateY(-1px);
        width: 1.5rem;
        height: 1px;
        background: #babec2;
      }
      .tips::after {
        content: "";
        display: block;
        position: absolute;
        right: -1.7rem;
        top: 50%;
        width: 1.5rem;
        transform: translateY(-1px);
        height: 1px;
        background: #babec2;
      }
    }
  }
}
</style>
<style lang="scss">
.page-mobile_study-level {
  .van-rate__icon {
    color: var(--theme-color);
  }
}
</style>
